package server

import (
	"errors"
	"fmt"
	jwt "github.com/golangblogs/gojwt"
	"github.com/google/uuid"
	"strconv"
	"time"
	"week53/common/model/mysql"
	"week53/common/proto/user"
	"week53/pkg"
)

// 用户注册
func Register(req *user.RegisterReq) (*user.RegisterResp, error) {
	u := mysql.User{
		Name:     req.Name,
		Password: req.Password,
	}
	err := u.Register()
	if err != nil {
		return nil, errors.New("注册失败")
	}
	c := mysql.Coupon{
		UserId:     int(u.ID),
		CouponName: "满100减5",
	}
	err = c.Add()
	if err != nil {
		return nil, errors.New("新人优惠卷发放失败")
	}
	return &user.RegisterResp{Success: "用户注册成功"}, nil
}

// 用户登录
func Login(req *user.LoginReq) (*user.LoginResp, error) {
	u := mysql.User{}
	login, err := u.Login(req.Name, req.Password)
	if err != nil {
		return nil, errors.New("登录失败")
	}
	token, err := jwt.NewJWT("zj").CreateToken(jwt.CustomClaims{ID: login.ID})
	if err != nil {
		return nil, errors.New("token 生成失败")
	}
	return &user.LoginResp{Success: token}, nil
}

// 添加课程
func AddClassPlan(req *user.AddClassPlanReq) (*user.AddClassPlanResp, error) {
	c := mysql.Class{}
	class, err := c.Find(int(req.ClassId))
	if err != nil {
		return nil, errors.New("没有这个课程")
	}
	cp := mysql.ClassPlan{
		UserId:  int(req.UserId),
		ClassId: int(class.ID),
		Name:    class.ClassName,
		Price:   class.ClassPrice,
	}
	err = cp.AddClassPlan()
	if err != nil {
		return nil, errors.New("课程添加失败")
	}
	return &user.AddClassPlanResp{Success: "学习计划课程增加成功"}, nil
}

// 删除课程
func DelClassPlan(req *user.DelClassPlanReq) (*user.DelClassPlanResp, error) {
	cp := mysql.ClassPlan{}
	err := cp.DelClassPlan(req.Id, req.UserId)
	if err != nil {
		return nil, errors.New("课程删除失败")
	}
	return &user.DelClassPlanResp{Success: "删除成功"}, nil
}

// 添加购物车，提交订单
func CarAndOrder(req *user.CarAndOrderReq) (*user.CarAndOrderResp, error) {
	c := mysql.Class{}
	class, err := c.Find(int(req.ClassId))
	if err != nil {
		return nil, errors.New("没有这个课程")
	}
	bc := mysql.BuyCar{
		UserId:  int(req.UserId),
		ClassId: int(class.ID),
		Name:    class.ClassName,
		Price:   class.ClassPrice,
		Num:     int(req.Num),
	}
	err = bc.Add()
	if err != nil {
		return nil, errors.New("购物车添加失败")
	}

	amount := class.ClassPrice * float64(req.Num)

	Sn := time.DateTime + uuid.New().String()
	o := mysql.Order{
		UserId:  int(req.UserId),
		ClassId: int(class.ID),
		CarId:   int(bc.ID),
		OrderSn: Sn,
		Name:    class.ClassName,
		Price:   class.ClassPrice,
		Num:     int(req.Num),
		Amount:  amount,
	}
	err = o.AddOrder()
	if err != nil {
		return nil, errors.New("订单创建失败")
	}

	co := mysql.Coupon{}
	findCoupon, err := co.Find(int(req.UserId))
	if err != nil {
		return nil, errors.New("没有优惠卷")
	}
	if findCoupon.ID != 0 && amount == 100 {
		amount = amount - 5
	}

	price := strconv.FormatFloat(amount, 'f', 2, 64)
	pay := pkg.NewAliPay()
	OrderUrl := pay.Pay(class.ClassName, Sn, price)
	fmt.Println(OrderUrl)

	if o.Types == 1 {
		pkg.Cron(Sn)
	}

	return &user.CarAndOrderResp{
		Success:  "下单成功",
		Amount:   float32(class.ClassPrice * float64(req.Num)), //总金额
		Discount: int32(amount - 5),                            //优惠金额
		Prices:   float32(amount),                              //实际支付金额
	}, nil
}
